package test;

import java.util.Random;
import java.util.Scanner;


public class NGuessNumber {

  public static void main(String[] args) {
    System.out.println("游戏玩法:根据提示,猜对4个数字和他们的数据即可获胜!");
    System.out.println("请输入4个0-9之间的数字:");
    Random ran = new Random();


    Integer sb;
    sb = ran.nextInt(9999 - 1000 + 1) + 1000;
    //System.out.println(sb);


    while (true) {
      //获取随机数
      String str = sb.toString();
      int[] shuijishu = new int[4];
      for (int i = 0; i < str.length(); i++) {
        Character ch = str.charAt(i);
        shuijishu[i] = Integer.parseInt(ch.toString());
      }
      //获得用户输入
      Scanner sc = new Scanner(System.in);
      Integer q = sc.nextInt();

      String newq = String.valueOf(q);
      int qLangth = newq.length();
      if (qLangth != 4) {
        System.out.println("你输入的数字不是4位，请重新输入：");
        continue;
      } else {
        String str1 = q.toString();
        int[] newq1 = new int[4];
        for (int i = 0; i < newq1.length; i++) {
          Character ch = str1.charAt(i);
          newq1[i] = Integer.parseInt(ch.toString());
        }
        int Aa = 0;
        int Bb = 0;
        for (int i = 0; i < shuijishu.length; i++) {
          if (newq1[i] == shuijishu[i]) {
            Aa += 1;
          } else {
            for (int j = 0; j < shuijishu.length; j++) {
              if (newq1[i] == shuijishu[j]) {
                Bb += 1;
              }
            }
          }
        }
        System.out.println("" + Aa + "A" + Bb + "B");
        if (Aa == 4) {
          System.out.println("恭喜！你赢了");
        } else if (Aa != 4) {
          System.out.println("很可惜，你输了");
        }
        //再次游戏
        System.out.println("是否继续游戏？(y/n)");
        Scanner sr = new Scanner(System.in);
        String qc = sr.next();
        if (qc.equals("y")) {
          sb = ran.nextInt(9999 - 1000 + 1) + 1000;
          System.out.println("游戏玩法:根据提示,猜对4个数字和他们的数据即可获胜!");
          System.out.println("请输入4个0-9之间的数字:");
        } else {
          System.out.println("游戏结束了！");
          break;
        }
        Aa = 0;
        Bb = 0;
        continue;
      }
    }
  }
}
